package com.mjk.common.web.getway.restful.prototal.ws;



import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.gson.GsonUtils;
import com.mjk.common.tools.springextend.BeeApplicationListener;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.restful.prototal.BaseGetwayInterceptor;
import com.mjk.common.web.getway.utils.RequstParaseUtils;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;

import java.util.HashMap;
import java.util.Map;
/**
 * 非SOAP Webservice  JAX-WS规范
 * RESTful Webservice JAX-RS规范
 * 
 * @author msi
 *
 */

@Path("/")
public class GetWayWebServiceImpl extends BaseGetwayInterceptor implements GetWayWebService, BeeApplicationListener {

	@POST
	@Path("{work}")
	@Produces({ MediaType.APPLICATION_JSON })
	public String doCommonWebService(@PathParam("work")String work,
									 @QueryParam("method")String method, @Context ServletContext servletContext,
									 @Context HttpServletRequest request, @Context HttpServletResponse response) {
		//解析inMsg
		Map<String,Object> params = RequstParaseUtils.getMapFromRequest(request);
		Map<String,Object> restParams = new HashMap<>();
		restParams.put(GetwayConsts.INSTANCE, work);
		restParams.put(GetwayConsts.METHOD, method);
		restParams.put(GetwayConsts.PROTOCOL, GetWayInterceptorType.WS.getProto());//设为ws协议
		GetWayInterceptorReq req = new GetWayInterceptorReq();
		//设置restful params
		Map<String,Object> restfulParams = new HashMap<>();
		restfulParams.put(GetwayConsts.PROTOCOL, GetWayInterceptorType.WS.getProto());
		req.setRestfulParams(restfulParams);
		req.setProtocal(getWayProtocal);
		req.setParams(params);
		req.setRestfulParams(restParams);
		req.setResponse(response);
		BeeResponse result = GetWayInterceptorType.getGetWayInvoker(req).invoker(chains.get(GetWayInterceptorType.WS.getProto()), req);
		return GsonUtils.bean2Json(result);
	}
}
